import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, Form, Input, Popconfirm, Row, Table} from 'antd';
import Constants from '../../utils/Constants';
import RightConstants from '../../utils/RightConstants';
import RightBox from '../../components/common/rightBox';
import SupplierProfitDetail from '../../components/supplier/supplierProfit/supplierProfitDetail';

const FormItem = Form.Item;

const supplierProfit = Form.create()(class supplierProfit extends React.Component{
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.dispatch({ type: 'supplierProfit/getListForPage', payload: { page: Constants.DEFAULT_PAGE } });
    }

    handleOpen = (id, disabled) => {
        this.props.dispatch({
            type: 'supplierProfit/getById',
            payload: { id ,
                onSuccess:() => {
                    let dataSource = this.props.supplierProfit.dataSource ? this.props.supplierProfit.dataSource : {};
                    if(dataSource.supplierId){
                        this.props.dispatch({
                            type: 'supplierProfit/getListBySupplierId',
                            payload: {
                                supplierId : dataSource.supplierId,
                            }
                        })
                    }
                }}
        });
        this.props.dispatch({ type: 'supplier/getSupplierList', payload: { supplierType: Constants.SUPPLIER_TYPE.FRAE} });  //查询物流供应商
        this.supplierProfitDetail.handleOpen(disabled);
    }

    handleDelete = id => {
        this.props.dispatch({
            type: 'supplierProfit/deleteById',
            payload: {
                id,
                onSuccess: this.handleSearch
            }
        })
    }

        handleSearch = flag => {
        let newPage = this.props.supplierProfit.page;
        if (flag) newPage.page = 1;
        this.props.dispatch({
            type: 'supplierProfit/getListForPage',
            payload: {
                ...this.props.form.getFieldsValue(),
                page: newPage
            }
        });
    };

    columns = [
        { title: '供应商编码', dataIndex: 'supplierCode', key: 'supplierCode'},
        { title: '费用编码', dataIndex: 'feeCode', key: 'feeCode'},
        { title: '费用名称', dataIndex: 'feeName', key: 'feeName'},
        { title: '费用利润', dataIndex: 'profit', key: 'profit  '},
        { title: '币种', dataIndex: 'currency', key: 'currency'},
        { title: '创建人', dataIndex: 'creator', key: 'creator'},
        {
            title: '操作',
            render: (record) => {
                let editLink = <RightBox resource={RightConstants.SUPPLIER.SUPPLIER_SUPPLIERPROFIT_EDIT} split={true}><a href="#" onClick={this.handleOpen.bind(this, record.id, false)}>编辑</a></RightBox>
                let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record.id)} okText="是" cancelText="否"
                    placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除?'}>
                    <a href="#">删除</a>
                </Popconfirm>
                deleteLink = <RightBox resource={RightConstants.SUPPLIER.SUPPLIER_SUPPLIERPROFIT_DELETE} split={true}>{deleteLink}</RightBox>
                let viewLink = <a href="#" onClick={this.handleOpen.bind(this, record.id, true)}>查看</a>
                return <span>{editLink}{deleteLink}{viewLink}</span>
            }
        }
    ];

    render(){
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };
        const { getFieldDecorator } = this.props.form;
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.supplierProfit.page.size,
            current: this.props.supplierProfit.page.page,
            total: this.props.supplierProfit.page.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.supplierProfit.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'supplierProfit/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.supplierProfit.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'supplierProfit/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            }
        }

        return (
            <div>
                <Row>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'供应商代码'}>
                            {getFieldDecorator('supplierCode')
                            (
                                <Input maxLength="20" placeholder="供应商代码" />
                            )}
                        </FormItem>
                    </Col>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'费用项目代码'}>
                            {getFieldDecorator('fareItemCode')
                            (
                                <Input maxLength="100" placeholder="费用项目代码" />
                            )}
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'费用项目名称'}>
                            {getFieldDecorator('fareItemName')
                            (
                                <Input maxLength="200" placeholder="费用项目名称" />
                            )}
                        </FormItem>
                    </Col>

                    <Col span={7} style={{ float: 'right' }}>
                        <Button type="primary" icon="search" style={{marginRight: 5}} onClick={this.handleSearch.bind(this,true)} size={'default'}>查询</Button>
                        <RightBox resource={RightConstants.SUPPLIER_SUPPLIERPROFIT_ADD}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleOpen.bind(this, null, false)} size={'default'}>添加供应商利润</Button>
                        </RightBox>
                    </Col>
                </Row>


            <Table loading={this.props.loading} rowKey='id' dataSource={this.props.supplierProfit.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
            <SupplierProfitDetail wrappedComponentRef={(inst) => this.supplierProfitDetail = inst} refreshList={this.handleSearch} />
            </div>
        );
    }
})

function mapStateToProps(state){
    return{
        loading: state.loading.models.supplierProfit, supplierProfit: state.supplierProfit
    };
}

export default connect(mapStateToProps)(supplierProfit);
